<?php

/*

  Action	$2 + 1 Potion

  +1 Card; +1 Action
  Reveal the top 4 cards of your deck. Put the revealed Coppers and Potions into your hand. Put the other cards back on top in any order.

 */

class Apothecary extends Card implements iAction, iKingdom, iAlchemy {

    public function __construct() {
        $price = new Price(2, 1);
        parent::__construct("Apothecary", $price);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+1 Card",
            "+1 Action",
            "Reveal the top 4 cards of your deck. Put the revealed Coppers and Potions into your hand. Put the other cards back on top in any order.");
    }

    public function play(Game $game, $isRealCard) {
        $game->addActions(1);
        $drawed_cards = $game->drawCards(1);
         ScreenDisplay::addScreenMessage($game, true, "get +1 action and draw " . nicePrint($drawed_cards), "gets +1 action and draws " . sizeof($drawed_cards) . " cards");

        $reordercards = array();
        $copper = new Copper();
        $potion = new Potion();
        $copper_key = $copper->getKey();
        $potion_key = $potion->getKey();

        $revealed_cards = array();
        $drawed_cards = array();

        for ($i = 0; $i < 4; $i++) {
            $card = $game->peekCardFromDrawDeck();
            if (!$card)
                break;

            $revealed_cards[] = $card;
            if ($card->getKey() == $copper_key || $card->getKey() == $potion_key) {
                $temp = $game->drawCards(1);
                $drawed_cards = array_merge($temp, $drawed_cards);
            } else {
                $card = $game->removeCardFromDrawDeck();
                $reordercards[] = $card;
            }
        }

         ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($revealed_cards) . " and put " . nicePrint($drawed_cards) . " in your hand", "reveals " . nicePrint($revealed_cards) . " and puts " . nicePrint($drawed_cards) . " in his hand");

        if (sizeof($reordercards) > 0) {
            $output = MySocket::GetUserReply($game, Input_Reorder, $reordercards, -1, -1, null, "Put cards back in any order");
            ScreenDisplay::addScreenMessage($game, true, "put " . nicePrint($output) . " on top of your deck", "puts " . sizeof($output) . " cards on top of his deck");
            $output = array_reverse($output);
            foreach ($output as $p) {
                $game->putCardOnTopOfDrawDeck($p);
            }
        }

        $game->cardActionFinished();
        return true;
    }
    
    public function boardSetup(Game $game) {
        $game->addPotion();
        return true;
    }

}

?>